<template>
  <div class="mod-coupon">
    <el-form :inline="true" :model="queryDto">
      <el-form-item>
        <el-input v-model="queryDto.couponName" placeholder="优惠券名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryDto.couponType" placeholder="优惠券类型" clearable>
          <el-option v-for="option in couponTypeList" :key="option.value" :label="option.key" :value="option.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryDto.useType" placeholder="使用范围" clearable>
          <el-option v-for="option in useTypeList" :key="option.value" :label="option.key" :value="option.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryDto.userLevel" placeholder="用户等级" clearable>
          <el-option v-for="option in levelList" :key="option.id" :label="option.name" :value="option.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryDto.publishStatus" placeholder="发行状态" clearable>
          <el-option v-for="option in publishStatusList" :key="option.value" :label="option.key" :value="option.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="queryDto.time" type="datetimerange" align="right" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss"/>
      </el-form-item>   
      <el-form-item>
        <el-button type="info" @click="resetFind()">重置</el-button>
        <el-button type="primary" @click="getDataList()">搜索</el-button>
        <el-button type="success" @click="addHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="id" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="couponName" header-align="center" align="center" label="券名"></el-table-column>
      <el-table-column prop="couponImg" header-align="center" align="center" label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.couponImg" style="width: 40px; height: 40px;">
        </template>
      </el-table-column>
      <el-table-column prop="couponType" header-align="center" align="center" label="类型">
        <template slot-scope="scope">
          <el-tag v-for="item in couponTypeList"  v-if="scope.row.couponType === item.value" size="small" type="success" :key="item.value">
             {{ item.key }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="couponNum" header-align="center" align="center" label="数量"></el-table-column>
      <el-table-column prop="couponPrice" header-align="center" align="center" label="金额"></el-table-column>
      <el-table-column prop="userLevel" header-align="center" align="center" label="用户等级">
        <template slot-scope="scope">
          <el-tag v-for="item in levelList"  v-if="scope.row.userLevel === item.id" size="small" type="success" :key="item.id">
             {{ item.name }}
          </el-tag>
        </template>
      </el-table-column>    
      <el-table-column prop="startTime" header-align="center" align="center" label="开始时间"></el-table-column>
      <el-table-column prop="endTime" header-align="center" align="center" label="结束时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="280" label="操作">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="viewInfo(scope.row.id)">详情</el-button>
          <el-button type="warning" size="mini" @click="updateHandle(scope.row.id)">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="queryDto.pageNum"
      :page-sizes="[10, 20, 50, 100]" :page-size="queryDto.pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        queryDto: {
          couponName: '',
          couponType: '',
          useType: '',
          userLevel: '',
          publishStatus: '',
          startTime: '',
          endTime: '',
          time: '',
          pageNum: 1,
          pageSize: 10
        },
        dataList: [],
        totalPage: 0,
        levelList: [],
        dataListSelections: [],
        couponTypeList: [],
        useTypeList: [],
        publishStatusList: [],
        dataListLoading: false
      }
    },
    activated () {
      this.getDataList()
      this.getEnums()
      this.getLevels()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        if (this.queryDto.time != null) {
          this.queryDto.startTime = this.queryDto.time[0]
          this.queryDto.endTime = this.queryDto.time[1]
        }
        this.$http({
          url: this.$http.adornUrl('/mall-coupon/coupon/info/pageList'),
          method: 'post',
          data: this.queryDto
        }).then(({data}) => {
          if (data.code === 200) {
            this.dataList = data.data.records
            this.totalPage = data.data.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 查看详情
      viewInfo (couponId) {
        this.$router.push({path: '/coupon-info', query: { couponId: couponId }})
      },
      // 修改
      updateHandle (couponId) {
        this.$router.push({path: '/coupon-update', query: { couponId: couponId }})
      },
      // 新增
      addHandle () {
        this.$router.push({path: '/coupon-add'})
      },
      // 每页数量变化
      sizeChangeHandle (val) {
        this.queryDto.pageSize = val
        this.queryDto.pageNum = 1
        this.getDataList()
      },
      // 跳转指定页码
      currentChangeHandle (val) {
        this.queryDto.pageNum = val
        this.getDataList()
      },
      // 勾选表格数据
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`是否进行${id ? '删除' : '批量删除'}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/mall-coupon/coupon/info/delete`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message({
                showClose: true,
                message: data.message,
                type: 'warning',
                center: true
              })
            }
          })
        }).catch(() => {})
      },
      // 获取全部等级
      getLevels () {
        this.$http({
          url: this.$http.adornUrl('/mall-user/user/level/queryList'),
          method: 'get'
        }).then(({data}) => {
          this.levelList = data.data
        })
      },
      // 获取全部枚举
      getEnums () {
        this.$http({
          url: this.$http.adornUrl('/mall-user/user/tool/getEnums'),
          method: 'get'
        }).then(({data}) => {
          this.couponTypeList = data.data.CouponTypeEnum
          this.useTypeList = data.data.UseTypeEnum
          this.publishStatusList = data.data.PublishStatusEnum
        })
      },
      // 重置搜索
      resetFind () {
        this.queryDto.couponName = ''
        this.queryDto.couponType = ''
        this.queryDto.useType = ''
        this.queryDto.userLevel = ''
        this.queryDto.publishStatus = ''
        this.queryDto.startTime = ''
        this.queryDto.endTime = ''
        this.queryDto.time = ''
        this.getDataList()
      }
    }
  }
</script>
